//
//  WithdrawViewController.m
//  Findif
//
//  Created by Mac on 2018/11/8.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "WithdrawViewController.h"
#import "AddBankViewController.h"

@interface WithdrawViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate>{
    
    NSInteger _bankIndex;
}

@property (nonatomic, weak)UIButton * bankButton;
@property (nonatomic, weak)UILabel * noBankLabel;
@property (nonatomic, weak)UILabel * bankLabel;
@property (nonatomic, weak)UILabel * cardNumLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, weak)UIView * bottomView;
@property (nonatomic, weak)UILabel * drawLabel;
@property (nonatomic, weak)UITextField * drawMoneyTextFiled;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * vailLabel;
@property (nonatomic, weak)UIButton * allDrawButton;
@property (nonatomic, weak)UIButton * drawButton;

@property (nonatomic, copy)NSString * typeid;
@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, strong)UIView * bgView;
@property (nonatomic, strong)UIPickerView * pickerView;

@end

@implementation WithdrawViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    _bankIndex = 0;
    
    [self setupNav];
    [self setupUI];
    [self setupPivkerView];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getBingBankListsResultWithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            self.dataArray = responseData[@"data"];
            NSDictionary * dict = self.dataArray.firstObject;
            
            self.noBankLabel.hidden = YES;
            self.bankLabel.hidden = NO;
            self.cardNumLabel.hidden = NO;
            
            self.typeid = dict[@"id"];
            self.bankLabel.text = dict[@"belongs_to_bank"];
            NSString * card = dict[@"bank_card_no"];
            NSString * showCardNo;
            if (card.length == 16) {
                
                showCardNo = [card substringFromIndex:12];
            }else{
                
                showCardNo = [card substringFromIndex:15];
            }
            self.cardNumLabel.text = [NSString stringWithFormat:@"尾号(%@)",showCardNo];
            [self changeButtonState];
        }else{
            
            self.noBankLabel.hidden = NO;
            self.bankLabel.hidden = YES;
            self.cardNumLabel.hidden = YES;
        }
    }];
}

- (void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.pickerView reloadAllComponents];
}

- (void)setupNav{
    
    self.title = @"提现";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{
    
    UIButton * bankButton = [UIButton new];
    
    bankButton.backgroundColor = ZRWhiteColor;
    bankButton.layer.cornerRadius = 5;
    bankButton.layer.masksToBounds = YES;
    [bankButton addTarget:self action:@selector(clickBankButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.bankButton = bankButton;
    
    [self.view addSubview:self.bankButton];
    
    UILabel * noBankLabel = [UILabel new];
    
    noBankLabel.text = @"您没有绑定银行卡，添加银行卡后再来试试吧！";
    noBankLabel.textColor = [UIColor lightGrayColor];
    noBankLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    noBankLabel.userInteractionEnabled = NO;
    
    self.noBankLabel = noBankLabel;
    
    [self.bankButton addSubview:self.noBankLabel];
    
    UILabel * bankLabel = [UILabel new];
    
    bankLabel.text = @"----";
    bankLabel.textColor = ZRBlackColor;
    bankLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    bankLabel.userInteractionEnabled = NO;
    
    self.bankLabel = bankLabel;
    
    [self.bankButton addSubview:self.bankLabel];
    
    
    UILabel * cardNumLabel = [UILabel new];
    
    cardNumLabel.text = @"----";
    cardNumLabel.textColor = [UIColor lightGrayColor];
    cardNumLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    cardNumLabel.userInteractionEnabled = NO;
    
    self.cardNumLabel = cardNumLabel;
    
    [self.bankButton addSubview:self.cardNumLabel];
    

    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    rightImageView.userInteractionEnabled = NO;
    
    self.rightImageView = rightImageView;
    
    [self.bankButton addSubview:self.rightImageView];
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = ZRWhiteColor;
    bottomView.layer.cornerRadius = 5;
    bottomView.layer.masksToBounds = YES;
    
    self.bottomView = bottomView;
    
    [self.view addSubview:self.bottomView];
    
    
    UILabel * drawLabel = [UILabel new];
    
    drawLabel.text = @"提现金额";
    drawLabel.textColor = [UIColor lightGrayColor];
    drawLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.drawLabel = drawLabel;
    
    [self.bottomView addSubview:self.drawLabel];
    
    
    UITextField * drawMoneyTextFiled = [[UITextField alloc] init];
    
    drawMoneyTextFiled.borderStyle = UITextBorderStyleNone;
//    drawMoneyTextFiled.backgroundColor = [UIColor clearColor];
//    drawMoneyTextFiled.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    drawMoneyTextFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    drawMoneyTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入提现金额" attributes:attrs];
    drawMoneyTextFiled.textColor = ZRBlackColor;
    drawMoneyTextFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    UILabel * symbolLabel = [UILabel new];
    symbolLabel.text = @"￥";
    symbolLabel.textColor = ZRBlackColor;
    symbolLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    [drawMoneyTextFiled.leftView addSubview:symbolLabel];
    
    [symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(drawMoneyTextFiled.leftView);
        make.centerX.mas_equalTo(drawMoneyTextFiled.leftView);
    }];
    
    
    drawMoneyTextFiled.leftViewMode = UITextFieldViewModeAlways;
    drawMoneyTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    drawMoneyTextFiled.keyboardType = UIKeyboardTypeDecimalPad;
    drawMoneyTextFiled.delegate = self;
    
    self.drawMoneyTextFiled = drawMoneyTextFiled;
    
    [self.bottomView addSubview:self.drawMoneyTextFiled];
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    self.lineView = lineView;
    
    [self.bottomView addSubview:self.lineView];
    
    
    UILabel * vailLabel = [UILabel new];
    
    vailLabel.text = [NSString stringWithFormat:@"可用余额 %@ 元",self.money];
    vailLabel.textColor = [UIColor lightGrayColor];
    vailLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.vailLabel = vailLabel;
    
    [self.bottomView addSubview:self.vailLabel];
    
    
    UIButton * allDrawButton = [UIButton new];
    
    [allDrawButton setTitle:@"全部提现" forState:UIControlStateNormal];
    [allDrawButton setTitleColor:ColorWithRGB(0x53d499) forState:UIControlStateNormal];
    allDrawButton.titleLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    [allDrawButton addTarget:self action:@selector(clickAllDrawButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.allDrawButton = allDrawButton;
    
    [self.bottomView addSubview:self.allDrawButton];
    
    UIButton * drawButton = [UIButton new];
    
    [drawButton setBackgroundColor:ColorWithRGB(0xFCF4B6)];
    [drawButton setTitle:@"预计1~3个工作日到账，确认提现" forState:UIControlStateNormal];
    [drawButton setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    [drawButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [drawButton addTarget:self action:@selector(clickDrawButton) forControlEvents:UIControlEventTouchUpInside];
    drawButton.titleLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    drawButton.enabled = NO;
    
    self.drawButton = drawButton;
    
    [self.view addSubview:self.drawButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.bankButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(kNavHeight + kStatusBarHeight + 10 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bankButton);
        make.trailing.mas_equalTo(self.bankButton).offset(-10 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
    }];
    
    [self.noBankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bankButton);
        make.leading.mas_equalTo(self.bankButton).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    [self.bankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bankButton);
        make.leading.mas_equalTo(self.bankButton).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    [self.cardNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bankButton);
        make.trailing.mas_equalTo(self.rightImageView.mas_leading).offset(-10 * SCREENH6);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.bankButton);
        make.top.mas_equalTo(self.bankButton.mas_bottom).offset(20 * SCREENH6);
        make.height.mas_equalTo(90 * SCREENH6);
    }];
    
    [self.drawLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.noBankLabel);
        make.top.mas_equalTo(self.bottomView).offset(10 * SCREENH6);
    }];
    
    [self.drawMoneyTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.drawLabel);
        make.trailing.mas_equalTo(self.rightImageView);
        make.height.mas_equalTo(35 * SCREENH6);
        make.top.mas_equalTo(self.drawLabel.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.drawMoneyTextFiled);
        make.top.mas_equalTo(self.drawMoneyTextFiled.mas_bottom);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.vailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.lineView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(5 * SCREENH6);
    }];
    
    [self.allDrawButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.vailLabel);
        make.leading.mas_equalTo(self.vailLabel.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.drawButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.bottomView.mas_bottom).offset(30 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(60 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-60 * SCREENH6);
        make.height.mas_equalTo(35 * SCREENH6);
    }];
}


- (void)clickBankButton{
    
    if (self.noBankLabel.hidden) {
      
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(0, kScreenH - kScreenH / 3, kScreenW, kScreenH / 3);
        }];
    }else{
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"您还没绑定银行卡，先去绑定银行卡"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                                  HiddenBar;
                                                                  AddBankViewController * addVC = [AddBankViewController new];
                                                                  
                                                                  [self.navigationController pushViewController:addVC animated:YES];
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}

- (void)clickAllDrawButton{
    
    self.drawMoneyTextFiled.text = [NSString stringWithFormat:@"%@",self.money];
    [self changeButtonState];
}


- (void)clickDrawButton{
    
    NSDictionary * dict;
    if ([self.state isEqualToString:@"1"]) {
        
        dict = @{@"bank_card_id":self.typeid,@"withdrawal_money":self.drawMoneyTextFiled.text,@"withdrawal_identity":self.state};
    }else{
        
        NSString * shop_id = DEF_GET_OBJECT(@"shopid");
        dict = @{@"bank_card_id":self.typeid,@"withdrawal_money":self.drawMoneyTextFiled.text,@"withdrawal_identity":self.state,@"merchants_id":shop_id};
    }
    
    
    [[ZRNetworkTool sharedTool] initiateWithdrawalResultWithInfo:dict WithBlock:^(BOOL success, id responseData) {
        if (success) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你提交提现成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

- (void)changeButtonState{
    
    if (self.drawMoneyTextFiled.text.length > 0 && self.typeid) {
        
        self.drawButton.enabled = YES;
        [self.drawButton setBackgroundColor:ColorWithRGB(0xFFE055)];
    }
}

- (void)setupPivkerView{
    
    self.bgView = [[UIView alloc]init];
    self.bgView.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH / 3);
    self.bgView.backgroundColor = ZRWhiteColor;
    [self.view addSubview:self.bgView];
    
    UIView *btnView = [[UIView alloc]init];
    btnView.frame = CGRectMake(0, 0, kScreenW, 40 * SCREENH6);
    btnView.backgroundColor = ColorWithRGB(0xababab);
    [self.bgView addSubview:btnView];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(10 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [sureBtn setTitle:@"取消" forState:UIControlStateNormal];
    [sureBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:sureBtn];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(self.view.frame.size.width - 70 * SCREENH6, 5 * SCREENH6, 60 * SCREENH6, 30 * SCREENH6);
    [cancelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(sureBtn) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:cancelBtn];
    
    float height = kScreenH / 3 - 40 * SCREENH6;
    self.pickerView = [[UIPickerView alloc]init];
    self.pickerView.frame = CGRectMake(0, 40 * SCREENH6, kScreenW, height);
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.bgView addSubview:self.pickerView];
}


// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.dataArray.count == 0) {
        
        self.noBankLabel.hidden = NO;
        self.bankLabel.hidden = YES;
        self.cardNumLabel.hidden = YES;
    }else{
        
        self.noBankLabel.hidden = YES;
        self.bankLabel.hidden = NO;
        self.cardNumLabel.hidden = NO;
    }
    return self.dataArray.count;
}


// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    NSDictionary * dict = self.dataArray[row];
    
    _bankIndex = row;
    NSString * card = dict[@"bank_card_no"];
    NSString * showCardNo;
    if (card.length == 16) {
        
        showCardNo = [card substringFromIndex:12];
    }else{
        
        showCardNo = [card substringFromIndex:15];
    }
    NSString * bankName = dict[@"belongs_to_bank"];
    
    NSString * bankInfo = [NSString stringWithFormat:@"%@(尾号%@)",bankName,showCardNo];
    
    return bankInfo;
}


- (void)cancelBtn{
    
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, kScreenW, kScreenH / 3);
    } completion:^(BOOL finished) {
    }];
    
}


- (void)sureBtn{
    
    NSDictionary * dict = self.dataArray[_bankIndex];
    self.typeid = dict[@"id"];
    
    self.bankLabel.text = dict[@"belongs_to_bank"];
    NSString * card = dict[@"bank_card_no"];
    NSString * showCardNo;
    if (card.length == 16) {
        
       showCardNo = [card substringFromIndex:12];
    }else{
        
      showCardNo = [card substringFromIndex:15];
    }
    
    self.cardNumLabel.text = [NSString stringWithFormat:@"尾号(%@)",showCardNo];
    
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, kScreenH / 3);
    } completion:^(BOOL finished) {
        
        
    }];
    
    [self changeButtonState];
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    [self changeButtonState];
}



@end
